package com.codebuffer.nowcoder;

import java.util.PriorityQueue;
import java.util.Stack;
import java.util.TreeSet;

public class KthNode {

    static Stack<TreeNode> stack = new Stack<>();
    static TreeNode KthNode(TreeNode pRoot, int k) {
        if(null==pRoot||k<1){
            return null;
        }

        TreeNode node = pRoot;
        while (stack.size()>0 || null!=node){
            if(null!=node){
                stack.add(node);
                node = node.left;
            }else {
                TreeNode pop = stack.pop();
                k--;
                if(k==0){
                    return pop;
                }
                node = pop.right;
            }

        }
        return null;
    }

}

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }
}
